package PermissionController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.commons.global.StaticCaches;
import com.renjun.smweb.commons.util.Encrypt;
import com.renjun.smweb.dao.pojo.LoginInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.renjun.smweb.controller.PermissionController.PermissionController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by huamin on 2017/2/23.
 * 权限管理M3
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath*:/spring-mvc.xml", "classpath*:/spring-mybatis.xml"})
public class PermissionControllerTestCase {

    @Autowired
    private PermissionController permissionController;

    //模拟request,response
    private MockHttpServletRequest request;
    private MockHttpServletResponse response;

    @Before
    public void setUp() throws Exception {
        request = new MockHttpServletRequest();
        request.getSession().setAttribute("userinfo", getLoginfo());
//        response = new MockHttpServletResponse();
    }

    //获取缓存LoginInfo
    private LoginInfo getLoginfo() {
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setId("999");
        loginInfo.setRoleid("0");
        loginInfo.setDistrictcode("310116");
        loginInfo.setRoletype("2");
        loginInfo.setUsername("测试123");
        loginInfo.setRealname("junit测试管理员");
        return loginInfo;
    }

    /**
     * M3-123query角色查询（角色，菜单，数据，接口）
     *
     * @param {roleid:"2"}
     * @return [{"roleid":"2","rolename":"市管理员","roledesc":"市管理员","roletype":"1","menus":[],"funcs":[]}]
     * by huamin
     */
    @Test
    public void testGetRoleConfigs(){
        System.out.println("M3-123query角色查询（角色，菜单，数据，接口）");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("roleid","999");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //M3-123query角色查询（角色，菜单，数据，接口）
        JSONObject rs = permissionController.getRoleConfigs(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    /**
     * M3-123update角色修改（角色，菜单，数据，接口）
     *
     * @param {roleid:"2","menus":[],"funcs":[],rolename:"2",roledesc:"2"}
     * @return
     * by huamin
     */
    @Test
    public void testConfigRole(){
        System.out.println("M3-123update角色修改（角色，菜单，数据，接口）");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("roleid","999");
        params.put("menus",new String[]{"test","index"});
        params.put("funcs",new String[]{"M2-3","M2-4"});
        params.put("rolename","测试员名称4");
        params.put("roledesc","测试员名称4");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //M3-123query角色查询（角色，菜单，数据，接口）
        JSONObject rs = permissionController.configRole(request, response);
        String str = JSON.toJSONString(rs);
        System.out.println("\n json对象：" + str + "\n");
    }

    /**
     * M3-1所有菜单列表
     *
     * @param {}
     * @return
     * by huamin
     */
    @Test
    public void testQueryAllMenus(){
        System.out.println("M3-1所有菜单列表");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //M3-1所有菜单列表
        JSONObject rs = permissionController.queryAllMenus(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    /**
     * M3-3所有功能列表
     *
     * @param {}
     * @return
     * by huamin
     */
    @Test
    public void testQueryAllFuncs(){
        System.out.println("M3-3所有功能列表");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //M3-1所有菜单列表
        JSONObject rs = permissionController.queryAllFuncs(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }
}
